//
//  CZOrginal.swift
//  项目框架
//
//  Created by yang on 16/3/2.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
let CellMargin = 10

class CZOrginal: UIView {
    /*
    原创微博 --
    头像
    认证图片
    微博名字
    会员等级
    发表时间
    发表来源
    发表内容
    */
    
    //定义一个约束记录底部
    var bottomConstarint: Constraint?
    //定义模型，方便赋值
    var statusViewModel:CZStatusViewModel?{
    
        didSet{
        
            //名字
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            //内容
            contentLabel.text = statusViewModel?.statusModel?.text
            
            //认证图片
            veriftyImageView.image = statusViewModel?.verifity_image
            
            //设置等级
            levelImageView.image = statusViewModel?.level_image
            
            //来源的内容
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.statusModel?
                .source)
            
            //显示时间
            timeLabel.text = statusViewModel?.dealCreateDate(statusViewModel?
                .statusModel?.created_at)
            //清空约束
            bottomConstarint?.uninstall()
            // 配图视图数组有配图,并且count > 0
            if let pic_urls = statusViewModel?.statusModel?.pic_urls
                where pic_urls.count > 0{
                
                print(statusViewModel?.statusModel?.text)
                
                pictureView.hidden = false
                
                    self.snp_updateConstraints(closure: { (make) -> Void in
                        //底部应该等于配图视图的底部
                        bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    })
                    
                    //定义一个临时数组接收
                    var result = [CZPictureViewModel]()
                    
                    //遍历
                    for model in pic_urls{
                    
                        let viewModel = CZPictureViewModel()
                        
                        viewModel.model = model
                        
                        result.append(viewModel)
                    }
                    
                    pictureView.pictureList = result
            } else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //底部应该等于配图视图的底部
                    bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
            }
        }
        
    }
    


    //自定义视图三部曲
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
    
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加配图视图
        addSubview(pictureView)
        
        //添加约束
        //头像约束 -- 左-10 上边 10 宽 40 高 40

        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)
            
            make.size.equalTo(CGSizeMake(40, 40))
        }
        //认证图片的约束 -- 中心点
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        // 名字 --头想左边 上边
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        //等级 -- 左边 中心线
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        // 时间的约束 -- 左 10,上边 5
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
        }
        
        // 来源 -- 左边 10 ,中心线
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        
        //设置内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
            //make.right.equalTo(self.snp_right).offset(CellMargin)
            //固定宽
            make.width.equalTo(UIScreen.mainScreen().bounds.width -
                2 * CGFloat(CellMargin))
        }
        
        /*
        按照添加转发微博的思路添加
        1.默认都有配图视图
        2.默认就由一张图片 -- 宽高是固定死的
        */
        // 配图视图的约束
        /*
        显示不显示配图视图
        当有配图的时候,展示配图,修改约束
        没有的时候,就隐藏,修改约束
        */
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
           // make.size.equalTo(CGSizeMake(100, 100))
        }
        //告诉视图的底部是哪里
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin)
        }
    }
    
    //懒加载
    
    //头像
    private lazy var photoImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    
    }()
    //认证图片
    private lazy var veriftyImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    
    //微博名字
    private lazy var nameLabel:UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.text = "微博名字"
        
        return label
    }()
    
    //会员等级
    private lazy var levelImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    //发表时间
    private lazy var timeLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(13)
        
        label.textColor = UIColor.orangeColor()
        
        return label
    }()
    
    //发表来源
    private lazy var sourceLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "微博来源"
        
        label.font = UIFont.systemFontOfSize(13)
        
        return label
        
    }()
    
    //发表内容
    private lazy var contentLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "微博内容"
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //转发微博的配图
    private lazy var pictureView:CZPictureView = CZPictureView()
}




















